
****************************************
****************************************
* THIS FILE REPLICATES THE EVIDENCE USING
* THE NEW ZEALAND DATA.
****************************************
****************************************

* IMPORTANT: make sure to run start.do before to set up the directories.  

/*------------------------- 
	Organization

		SECTION 1. Clean New Zealand data
		
		SECTION 2: Law of motion for markups
		
-------------------------*/ 

/**********************************************************************/
/*  SECTION 1. Clean New Zealand data

	- 1.1 Variables construction
	- 1.2 Descriptives
*/
/**********************************************************************/


clear all

use "${data}/NZ_master_file.dta", clear

 
/*----------------------------------------------------*/
   /* [>   1.1 Variables construction   <] */ 
/*----------------------------------------------------*/

gen w2_edsales_12m = w2_esales_12m_more25 * 0.25  ///
				   + w2_esales_12m_15_25 * 0.2 ///
			       + w2_esales_12m_5_15 * 0.1 ///
			       + w2_esales_neg15_neg5 * (-0.10) ///	
			       + w2_esales_neg25_neg15 * (-0.20) ///
			       + w2_esales_moreneg25 * (-0.25)

label var w2_edsales_12m "Expected change in sales in next 12-months"

* create edsales with triangular tales
gen temp_ar = w2_esales_12m_15_25
gen temp_br = 2 * w2_esales_12m_more25/temp_ar if temp_ar > 0
replace temp_br = 0 if temp_ar == 0 & w2_esales_12m_more25 == 0
replace temp_br = 1 if temp_ar == 0 & w2_esales_12m_more25 > 0
gen temp_al = w2_esales_neg25_neg15
gen temp_bl = 2 * w2_esales_moreneg25/temp_al if temp_al > 0
replace temp_bl = 0 if temp_al == 0 & w2_esales_moreneg25 == 0
replace temp_bl = 1 if temp_al == 0 & w2_esales_moreneg25 > 0

gen w2_edsales_tri = (temp_al * temp_bl * (9*25-4*temp_bl)/6 ///
			       - 20 * temp_al ///
			       - 10 * w2_esales_neg15_neg5 ///
			       + 10 * w2_esales_12m_5_15 ///
			       + 20 * w2_esales_12m_15_25 ///
			       + temp_ar * temp_bl * (temp_bl+3*25)/6)/100

label var w2_edsales_tri "Expected change in sales (triangular tales)"


* Current profit margin - Average profit margin, demeaned
gen mu_hat = w2_margin_current - w1_margin_average
egen mean_mu = mean(mu_hat)
gen dmu_hat = mu_hat - mean_mu
drop mean_mu

* Expected size of next price change, demeaned
egen mean_price = mean(w2_edp_size_main)
gen w2_dedp_size_main = w2_edp_size_main - mean_price
drop mean_price

* Expected change in sales (triangular tales), demeaned
egen mean_sales = mean(w2_edsales_tri)
gen w2_dedsales_tri = w2_edsales_tri - mean_sales
drop mean_sales

* Absloute values
gen abs_ded = abs(w2_dedsales_tri)
gen abs_mu = abs(dmu_hat)


* industry
encode agg2, gen(agg2_num)

 
/*----------------------------------------------------*/
   /* [>   1.2 Descriptives   <] */ 
/*----------------------------------------------------*/

* Table 6: Descriptive statistics for New Zealand data
tempfile meandescriptives sddescriptives mediandescriptives p25descriptives p75descriptives

preserve
	collapse (mean) w2_actual_output_all w2_edsales_12m w2_margin_current w2_edp_size_main
	gen Stats = "Mean"
	order Stats w2_actual_output_all w2_edsales_12m w2_margin_current w2_edp_size_main
	save "`meandescriptives'", replace
restore

preserve
	collapse (p25) w2_actual_output_all w2_edsales_12m w2_margin_current w2_edp_size_main
	gen Stats = "P 25"
	order Stats w2_actual_output_all w2_edsales_12m w2_margin_current w2_edp_size_main
	save "`p25descriptives'", replace
restore

preserve
	collapse (p50) w2_actual_output_all w2_edsales_12m w2_margin_current w2_edp_size_main
	gen Stats = "P 50"
	order Stats w2_actual_output_all w2_edsales_12m w2_margin_current w2_edp_size_main
	save "`mediandescriptives'", replace
restore

preserve
	collapse (p75) w2_actual_output_all w2_edsales_12m w2_margin_current w2_edp_size_main
	gen Stats = "P 75"
	order Stats w2_actual_output_all w2_edsales_12m w2_margin_current w2_edp_size_main
	save "`p75descriptives'", replace
restore

preserve
	collapse (sd) w2_actual_output_all w2_edsales_12m w2_margin_current w2_edp_size_main
	gen Stats = "SD"
	order Stats w2_actual_output_all w2_edsales_12m w2_margin_current w2_edp_size_main
	save "`sddescriptives'", replace
restore

preserve
	use "`meandescriptives'", clear
	append using "`p25descriptives'"
	append using "`mediandescriptives'"
	append using "`p75descriptives'"
	append using "`sddescriptives'"

	foreach var of varlist w2_actual_output_all w2_edsales_12m w2_margin_current w2_edp_size_main {

		gen temp = strofreal(`var', "%10.2f")
		drop `var'
		rename temp `var'
	}

	label var Stats "Statistic"
	label var w2_actual_output_all "Firm production value"
	label var w2_edsales_12m "Expected % change in sales"
	label var w2_margin_current "Markup"
	label var w2_edp_size_main "Expected size of next price change"

	texsave Stats w2_actual_output_all w2_edsales_12m w2_margin_current w2_edp_size_main ///
		using "${tables}/descriptives_NZ.tex", replace nofix hlines(1(1)5) ///
		varlabels location("H") frag title("Descriptive statistics for Compustat data") ///
		footnote("The table provides summary statistics for the survey of firms' expectations from New Zealand. The dataset contains 3,153 observations for 3,153 firms in 18 industries and corresponds to the first wave of the survey conducted by \cite{Coibion2014}.")
restore

* Figure 7: Number of competitors histogram
histogram w1_competitors, fraction width(1) ///
	graphregion(color(white)) plotregion(color(white)) ///
	xtitle("Sales") color("0 0 0%70") 
graph export "${figures}/NumbCompetitorsHistogram.pdf", replace



/**********************************************************************/
/*  SECTION 2:  Law of motion for markups  			
    Notes: 

	- 2.1 Table 7: Law of Motion for Markups: Survey Data from New Zealand
	- 2.2 Figure 6: Different cutoffs for N in regression for New Zealand

*/
/**********************************************************************/

 
/*----------------------------------------------------*/
   /* [>   2.1 Table 7: Law of Motion for Markups: Survey Data from New Zealand   <] */ 
/*----------------------------------------------------*/
eststo clear

eststo: reg dmu_hat w2_dedp_size_main w2_dedsales_tri i.agg2_num  ///
	if w1_competitors <= 20 & w1_competitors > 2 & abs_ded < 14,  robust

estadd  local indusfixedeffects "Yes"
estadd  local ncompetitors "2 $\leq$ competitors $\leq$ 20"
	
eststo: reg dmu_hat w2_dedp_size_main w2_dedsales_tri i.agg2_num ///
	if w1_competitors > 20,  robust
estadd  local indusfixedeffects "Yes"
estadd  local ncompetitors "2 $>$ 20"

esttab using "${tables}/MainRegression_NZ.tex", ///
	replace b(%4.3f) tex se star(* .1 ** .05 *** .01) noconstant /// 
	keep(w2_dedp_size_main w2_dedsales_tri) nofix ///
	coeflabels (dmu_hat "Markup" w2_dedp_size_main "Expected size of next price change" ///
	w2_dedsales_tri "Expected growth in sales") ///
	mtitles("Markup" "Markup") ///
	scalars("indusfixedeffects Industry FE" "ncompetitors Number of competitors") ///
	nonotes r2(%4.3f) obslast compress ///
	title("Law of Motion for Markups: Survey Data from New Zealand")

eststo clear





 
/*----------------------------------------------------*/
   /* [>   2.2 Figure 6: Different cutoffs for N in regression for New Zealand <] */ 
/*----------------------------------------------------*/


*****************************
* heterogeneity analysis
*****************************


* different cutoffs for the number of competitors
gen cutoff = .
gen coef_price = .
gen ci_upper_price = .
gen ci_lower_price = .
gen coef_sales = .
gen ci_upper_sales = .
gen ci_lower_sales = .
gen ci_upper_price_1se = .
gen ci_lower_price_1se = .
gen ci_upper_sales_1se = .
gen ci_lower_sales_1se = .

foreach num of numlist 5(1)30 {

	reg dmu_hat w2_dedp_size_main w2_dedsales_tri i.agg2_num ///
		if w1_competitors <= `num' & w1_competitors > 2 & abs_ded < 14,  robust

	lincom w2_dedp_size_main
	replace coef_price = `r(estimate)' in `num'
	replace ci_upper_price = `r(estimate)'+2*`r(se)' in `num'
	replace ci_lower_price = `r(estimate)'-2*`r(se)' in `num'

	replace ci_upper_price_1se = `r(estimate)'+`r(se)' in `num'
	replace ci_lower_price_1se = `r(estimate)'-`r(se)' in `num'
	
	lincom w2_dedsales_tri

	replace coef_sales = `r(estimate)' in `num'
	replace ci_upper_sales = `r(estimate)'+2*`r(se)' in `num'
	replace ci_lower_sales = `r(estimate)'-2*`r(se)' in `num'
	replace ci_upper_sales_1se = `r(estimate)'+`r(se)' in `num'
	replace ci_lower_sales_1se = `r(estimate)'-`r(se)' in `num'

	replace cutoff = `num' in `num'
}


graph twoway ///
	(line coef_price cutoff, ///
		lcolor("0 0 0")) ///
	(line ci_lower_price_1se cutoff, ///
		color("0 0 0%80") lpattern(shortdash))  ///	
	(line ci_lower_price cutoff, ///
		color("0 0 0%80") lpattern(dash))  ///
	(line ci_upper_price cutoff, ///
		color("0 0 0%80") lpattern(dash))  ///
	(line ci_upper_price_1se cutoff, ///
		color("0 0 0%80") lpattern(shortdash)), ///
	graphregion(color(white)) ///
	ytitle("Effect of expected size of next price change (demeaned)", size(small)) ///
	xtitle("Numbers of competitors cutoff") ///
	xlabel(5(2)30, labsize(vsmall)) ///
	legend(order(1 "Estimates" 2 "One standard error" 3 "Two standard errors"))
graph export "${figures}/expectedpricechange_diffcutoffs.pdf", replace

graph twoway ///
	(line coef_sales cutoff, ///
		lcolor("0 0 0") lwidth(thin)) ///
	(line ci_lower_sales_1se cutoff, ///
		color("0 0 0%80") lpattern(shortdash)) ///
	(line ci_lower_sales cutoff, ///
		color("0 0 0%80") lpattern(dash)) ///
	(line ci_upper_sales_1se cutoff, ///
		color("0 0 0%80") lpattern(shortdash)) ///
	(line ci_upper_sales cutoff, ///
		color("0 0 0%80") lpattern(dash)), ///
	graphregion(color(white)) ///
	ytitle("Effect of expected growth in sales (demeaned) ", size(small)) ///
	xtitle("Numbers of competitors cutoff") ///
	xlabel(5(2)30, labsize(vsmall)) ///
	legend(order(1 "Estimates" 2 "One standard error" 3 "Two standard errors"))
graph export "${figures}/expectedgrowthsales_diffcutoffs.pdf", replace


/*------------------------------------ End of SECTION 2 ------------------------------------*/




